<template>
  <el-dialog title="商品查询" :close-on-click-modal="false" :visible.sync="visible" top="-20px" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <div class="text item">
      <el-form inline size="mini">
        <el-form-item label="商品类别">
          <el-select v-model="dataForm.ctgId" :disabled="disabled">
            <el-option v-for="item in this.ctgList" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" :disabled="disabled">
            <el-button slot="append" @click="openDialog('supply')" :disabled="disabled">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName">
            <el-button slot="append" @click="openDialog('brand')">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品代码">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="商品条码">
          <el-input v-model="dataForm.barcode"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="厂家色号" v-if="[1,6,8].includes(dataForm.ctgId)">
          <el-input v-model="dataForm.manufacturerColorNumber"></el-input>
        </el-form-item>
        <el-form-item label="镜架尺寸" v-if="[1,6,8].includes(dataForm.ctgId)">
          <el-input v-model="dataForm.goodsSize"></el-input>
        </el-form-item>
        <el-form-item label="镜架材质" v-if="dataForm.ctgId == 1">
          <el-select v-model="dataForm.frameMaterial" clearable>
            <el-option v-for="item in frameMaterialData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工艺类型" v-if="dataForm.ctgId == 1">
          <el-select v-model="dataForm.craftType" clearable>
            <el-option v-for="item in craftTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配件类型" v-if="dataForm.ctgId == 2">
          <el-select v-model="dataForm.accessoriesType" clearable>
            <el-option v-for="item in accessoryTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="球镜范围" v-if="[3,4].includes(dataForm.ctgId)">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.mirrorBallMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.mirrorBallMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="柱镜范围" v-if="[3,4].includes(dataForm.ctgId)">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.columnMirrorMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.columnMirrorMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="下加光" v-if="dataForm.ctgId == 3">
          <el-row>
            <el-col :span="11">
              <el-input v-model="dataForm.toTheLightMin"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center;"> - </el-col>
            <el-col :span="11">
              <el-input v-model="dataForm.toTheLightMax"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="定做类型" v-if="[3,4].includes(dataForm.ctgId)">
          <el-select v-model="dataForm.customizeType" >
            <el-option v-for="item in bizData.customizeTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材料分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.lensMaterial" clearable>
            <el-option v-for="item in bizData.lensMaterial" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜片分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.photometricClassification" clearable>
            <el-option v-for="item in bizData.photometricClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜片功能" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.lensFunction" clearable>
            <el-option v-for="item in lensFunction" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渐进片分类" v-if="dataForm.ctgId == 3">
          <el-select v-model="dataForm.progressiveSliceClassification" clearable>
            <el-option v-for="item in bizData.progressiveSliceClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="曲率" v-if="dataForm.ctgId == 4">
          <el-input v-model="dataForm.curvature"></el-input>
        </el-form-item>
        <el-form-item label="直径" v-if="dataForm.ctgId == 4">
          <el-input v-model="dataForm.diameter"></el-input>
        </el-form-item>
        <el-form-item label="使用类型" v-if="dataForm.ctgId == 4">
          <el-select v-model="dataForm.useType" clearable>
            <el-option v-for="item in bizData.useType" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="抛弃型类型" v-if="dataForm.ctgId == 4">
          <el-select v-model="dataForm.disusedClassification" clearable>
            <el-option v-for="item in bizData.disusedClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主容量" v-if="dataForm.ctgId == 5">
          <el-input v-model="dataForm.primaryCapacity"></el-input>
        </el-form-item>
        <el-form-item label="次容量" v-if="dataForm.ctgId == 5">
          <el-input v-model="dataForm.secondaryCapacity"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="dataForm.spec"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider content-position="left">查询数据</el-divider>

    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-button icon="el-icon-check" type="primary" @click="checkItemData" size="mini">选 择</el-button>
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" border style="margin-bottom: 15px;" header-cell-class-name="miui-header-cell-style" v-loading="dataListLoading" @selection-change="handleSelectionChange" @select="onTableSelect">
      <el-table-column key="1" type="selection" width="50"></el-table-column>
      <el-table-column key="2" prop="code" label="商品代码" width="180"></el-table-column>
      <el-table-column key="3" prop="viewGoodsName" label="商品名称"></el-table-column>
      <el-table-column key="4" prop="spec" label="型号" v-if="![3,4].includes(dataForm.ctgId)" width="60"></el-table-column>
      <el-table-column key="5" label="单位" width="60">
        <template slot-scope="scope">
          {{getValueByKey(unitTypeData,scope.row.unitType)}}
        </template>
      </el-table-column>
      <el-table-column key="6" prop="mirrorBall" label="老花镜度数" v-if="dataForm.ctgId == 8" width="100"></el-table-column>
      <el-table-column key="7" prop="manufacturerColorNumber" label="厂家色号" v-if="[1,6,8].includes(dataForm.ctgId)" width="80"></el-table-column>
      <el-table-column key="8" label="镜架材质" width="80" v-if="[1].includes(dataForm.ctgId)">
        <template slot-scope="scope">
          {{getValueByKey(frameMaterialData,scope.row.frameMaterial)}}
        </template>
      </el-table-column>
      <el-table-column key="9" prop="goodsSize" label="镜架尺寸" width="80" v-if="[1,6,8].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="10" label="配件型" v-if="dataForm.ctgId == 2" width="80">
        <template slot-scope="scope">
          {{getValueByKey(accessoryTypeData,scope.row.accessoriesType)}}
        </template>
      </el-table-column>
      <!-- 镜片 -->
      <el-table-column key="11" prop="mirrorBall" width="80" label="球镜" v-if="[3,4].includes(dataForm.ctgId) && dataForm.customizeType == '0'"></el-table-column>
      <el-table-column key="12" prop="columnMirror" width="80" label="柱镜" v-if="[3,4].includes(dataForm.ctgId) && dataForm.customizeType == '0'"></el-table-column>
      <el-table-column key="13" prop="toTheLight" width="80" label="下加光" v-if="dataForm.ctgId == 3 && dataForm.customizeType == '0'"></el-table-column>
      <el-table-column key="11" label="球镜区间"  width="80" v-if="[3,4].includes(dataForm.ctgId) && dataForm.customizeType == '1'">
        <template slot-scope="scope">
          <span>{{scope.row.customizedMirrorBallMin}}/{{scope.row.customizedMirrorBallMax}}</span>
        </template>
      </el-table-column>
      <el-table-column key="12" label="柱镜区间"  width="80" v-if="[3,4].includes(dataForm.ctgId) && dataForm.customizeType == '1'">
        <template slot-scope="scope">
          <span>{{scope.row.customizedColumnMirrorMin}}/{{scope.row.customizedColumnMirrorMax}}</span>
        </template>
      </el-table-column>
      <el-table-column key="13" label="下加光区间"  width="80" v-if="dataForm.ctgId == 3 && dataForm.customizeType == '1'">
        <template slot-scope="scope">
          {{scope.row.toTheLightMin}}/{{scope.row.toTheLightMax}}
        </template>
      </el-table-column>
      <el-table-column key="14" prop="refractiveIndex" label="折射率" width="60" v-if="dataForm.ctgId == 3"></el-table-column>
      <el-table-column key="15" label="镜片分类" width="80" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.photometricClassification,scope.row.photometricClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="16" label="材料分类" width="80" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.lensMaterial,scope.row.lensMaterial)}}
        </template>
      </el-table-column>
      <el-table-column key="17" label="渐进片分类" width="100" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(bizData.progressiveSliceClassification,scope.row.progressiveSliceClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="18" label="镜片功能" width="80" v-if="dataForm.ctgId == 3">
        <template slot-scope="scope">
          {{getValueByKey(lensFunction,scope.row.lensFunction)}}
        </template>
      </el-table-column>
      <el-table-column key="19" prop="curvature" label="曲率" width="60" v-if="dataForm.ctgId == 4"></el-table-column>
      <el-table-column key="20" prop="diameter" label="直径" width="60" v-if="dataForm.ctgId == 4"></el-table-column>
      <el-table-column key="21" label="使用类型" v-if="dataForm.ctgId == 4" width="80">
        <template slot-scope="scope">
          {{getValueByKey(bizData.useType,scope.row.useType)}}
        </template>
      </el-table-column>
      <el-table-column key="22" label="抛弃类型" v-if="dataForm.ctgId == 4" width="80">
        <template slot-scope="scope">
          {{getValueByKey(bizData.disusedClassification,scope.row.disusedClassification)}}
        </template>
      </el-table-column>
      <el-table-column key="23" prop="primaryCapacity" label="主容量" width="80" v-if="dataForm.ctgId == 5"></el-table-column>
      <el-table-column key="24" prop="secondaryCapacity" label="次容量" width="80" v-if="dataForm.ctgId == 5"></el-table-column>
    </el-table>
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyId"></search-supply>
    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandId"></search-brand>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams, uniqObjArrById } from '@/util/tools'
  import { remote } from '@/api/admin/dict'
  import { goodsFetchList } from '@/api/eims/goods'
  import { getCtgList } from '@/api/eims/ctg'

  import SearchSupply from '@/components/business/SearchSupply'
  import SearchBrand from '@/components/business/SearchBrand'
  export default {
    // props: ["ctgList"],
    data() {
      return {
        visible: false,
        disabled: false,
        formData: [],
        unitTypeData: [],
        frameMaterialData: [],
        craftTypeData: [],
        lensFunction: [],
        accessoryTypeData: [],
        dataForm: {
          ctgId: '',
          supplyId: '',
          customizeType: '0',
          barcode: '',
          supplyName: '',
          brandId: '',
          brandName: '',
          spec: ''
        },
        selected: true,
        rowItem: {},
        ctgList: [],
        dataList: [],
        dataListLoading: false,
        multipleSelection: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        supplyVisible: false,
        brandVisible: false
      }
    },
    components:{
      SearchSupply,
      SearchBrand
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      /*
      * data   已选的商品列表(必填，可为空数组)
      * params  查询条件(必填，可为空对象)
      * disabled  商品类别和制造商是否可选(必填，布尔值)
      */
      init(data) {
        this.visible = true
        this.pageIndex = 1
        remote('unit_type').then(result => {
          this.unitTypeData = result.data.data
        })
        remote('frame_material').then(result => {
          this.frameMaterialData = result.data.data
        })
        remote('craft_type').then(result => {
          this.craftTypeData = result.data.data
        })
        remote('lens_function').then(result => {
          this.lensFunction = result.data.data
        })
        remote('accessory_type').then(result => {
          this.accessoryTypeData = result.data.data
        })
        this.formData = data.data
        this.dataForm = data.params
        this.disabled = data.disable
        this.$set(this.dataForm,'customizeType','0')
				if(data.disable) {
					this.disabled = true
				}
        this.multipleSelection = data.data;
        this.getDataList()
        this.getGoodsCtgList()
      },
			//	查询数据
			searchHandle() {
				this.pageIndex = 1
				this.getDataList()
			},
      //	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        goodsFetchList(Object.assign(params1, params2)).then(result => {
          let data = result.data.data.records
          data.forEach(item => {
            item.goodsId = item.id
          })
          this.dataList = data
          this.totalPage = result.data.data.total
          this.$nextTick(() => {
            this.multipleSelection.forEach(item => {
              let idVal = item.goodsId
              for (let i = 0; i < this.dataList.length; i++) {
                if (idVal == this.dataList[i].goodsId) {
                  this.$refs.singleTable.toggleRowSelection(this.dataList[i], true)
                }
              }
            })
          })
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //  判断当前操作的属性是勾选还是取消勾选
      onTableSelect(rows,row) {
        this.selected = rows.length && rows.indexOf(row) !== -1
        this.rowItem = row
      },
      handleSelectionChange(val) {
        if(this.selected){
          val.forEach(item => {
            let result = this.multipleSelection.some(term => {
              if(term.goodsId == item.goodsId){
                return true
              }
            })
            if(!result){
              this.multipleSelection.push(item)
            }
          })
        }else{
          let that = this
          this.multipleSelection = this.multipleSelection.filter(function(item){
            return item.goodsId != that.rowItem.goodsId
          })
        }
      },
      //  选择并关闭
      checkItemData() {
        this.multipleSelection.forEach((item, index) => {
          if (this.formData.length) {
            for (let i = 0; i < this.formData.length; i++) {
              let idVal = this.formData[i].goodsId
              if (item.goodsId == idVal) {
                item.quantity = this.formData[i].quantity
                item.applyQuantity = this.formData[i].applyQuantity
                item.memo = this.formData[i].memo
                if (item.ctgId == '4') {
                  item.invalidTimeStr = this.formData[i].invalidTimeStr
                  item.batch = this.formData[i].batch
                  item.registerNum = this.formData[i].registerNum
                }
              }
            }
          } else {
            item.quantity = ''
            item.applyQuantity = ''
            item.memo = ''
            if (item.ctgId == '4') {
              item.invalidTimeStr = ''
              item.batch = ''
              item.registerNum = ''
            }
          }
        })
        this.visible = false
        this.$emit('updateForm', this.multipleSelection)
      },
      openDialog(type) {
        if(type == 'supply'){
          if(this.dataForm.ctgId){
            let data = { ctgId: this.dataForm.ctgId }
            this.supplyVisible = true
            this.$nextTick(() => {
              this.$refs.supplyDialog.init(data)
            })
          }else{
            this.$message.warning('请选择采购类型')
          }
        }else if(type == 'brand'){
          if(this.dataForm.supplyId){
            let data = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
            this.brandVisible = true
            this.$nextTick(() => {
              this.$refs.brandDialog.init(data)
            })
          }else{
            this.$message.warning('请选择制造商')
          }

        }
      },
      getSupplyId(data) {
        this.$set(this.dataForm,'supplyName',data.name)
        this.dataForm.supplyId = data.id
      },
      getBrandId(data) {
        this.dataForm.brandId = data.id
        this.$set(this.dataForm,'brandName',data.name)
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //  获取商品列表
      getGoodsCtgList() {
        getCtgList().then(result => {
          this.ctgList = result.data.data
        })
      }
    }
  }
</script>

<style>
</style>
